/*
 * @Description: 
 * @Version: 1.0
 * @Autor: etonetech
 * @Date: 2023-02-02 17:22:43
 * @LastEditors: etonetech
 * @LastEditTime: 2023-02-09 17:00:30
 */
import Cookies from 'js-cookie'
import Encrypt from '@/utils/encrypt'
const TokenKey = import.meta.env.VITE_APP_RUN_ENV != 'prod' ? 'uiap-t' : 'CIOT_UIAP_TK'
const RTokenKey = 'uiap-r'
const expires = 'uiap-expires'
const UserKey = 'uiap-u'
const RoleKey = 'uiap-role'
const existotherToken = 'uiap-e'
const systemStore = 'x-ciot-uss'

// 统一设置
export function setSystemStore(key: string, value: any) {
  const systemStoreObj = getSystemStore()
  systemStoreObj[key] = value
  return localStorage.setItem(systemStore, Encrypt.randomEncode(JSON.stringify(systemStoreObj)))
}

export function getSystemStore() {
  if (!localStorage.getItem(systemStore)) {
    return {}
  }
  return JSON.parse(Encrypt.randomDecode(localStorage.getItem(systemStore)!))
}

// 是否存在同账号登录
export function getExistToken() {
  return getSystemStore()[existotherToken]
  // return Encrypt.randomDecode(localStorage.getItem(existotherToken))
  // return Cookies.get(existotherToken)
}
export function setExistToken(e: any) {
  return setSystemStore(existotherToken, e)
  // return localStorage.setItem(existotherToken,Encrypt.randomEncode(e))
  // return Cookies.set(existotherToken,e)
}

export function getExpires() {
  return getSystemStore()[expires]

  // return localStorage.getItem(expires)
  // return Cookies.get(expires)
}
export function setExpires(e: any) {
  return setSystemStore(expires, e)
  // return localStorage.setItem(expires,e)
  // return Cookies.set(expires,e)
}

export function getRefreshToken() {
  return getSystemStore()[RTokenKey]

  // return localStorage.getItem(RTokenKey)
  // return Cookies.get(RTokenKey)
}

export function setRefreshToken(token: string) {
  return setSystemStore(RTokenKey, token)

  // return localStorage.setItem(RTokenKey,token)
  // return Cookies.set(RTokenKey, token)
}

export function setToken(token: string, expires: number) {
  return Cookies.set(TokenKey, Encrypt.randomEncode(token), expires || 3600)
}

export function getToken() {
  return import.meta.env.NODE_ENV != 'prod' ? Encrypt.randomDecode(Cookies.get(TokenKey)) : Cookies.get(TokenKey)
}

export function removeToken() {
  Cookies.remove(TokenKey)
  Cookies.remove(UserKey)
  Cookies.remove(RoleKey)
  Cookies.remove(RTokenKey)
  Cookies.remove(expires)
  Cookies.remove(existotherToken)
  Cookies.remove('CIOT_UIAP_TK')
  localStorage.removeItem(expires)
  localStorage.removeItem(RTokenKey)
  localStorage.removeItem(existotherToken)
  localStorage.removeItem(systemStore)
  localStorage.removeItem('menuKey')
  localStorage.removeItem('loginName')
  localStorage.removeItem('isReturn')
  localStorage.removeItem('ciot_target')
  return
}

export function getUserInfo() {
  const userInfo = Cookies.get(UserKey)
  return userInfo ? JSON.parse(userInfo) : {}
}

export function setUserInfo(user: any) {
  return Cookies.set(UserKey, JSON.stringify(user))
}
